/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.arqsw.servicios;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import uy.edu.ort.arqsw.domino.Tag;

/**
 *
 * @author victor
 */
@Stateless
public class TagFacade implements TagFacadeLocal {
    @PersistenceContext(unitName = "obligContenidoWeb-ejbPU")
    private EntityManager em;

    @Override
    public void crear(Tag tag) {
        em.persist(tag);
    }

    @Override
    public void editar(Tag tag) {
        em.merge(tag);
    }

    @Override
    public void remover(Tag tag) {
        em.remove(em.merge(tag));
    }

    @Override
    public Tag buscar(String valor) {
        String strQuery = "SELECT t FROM Tag t WHERE t.valor = :valor";
        Query query = em.createQuery(strQuery).
                            setParameter("valor", valor);
        
        List<Tag> res = query.getResultList();
        
        if (res.size() > 0) {
            return res.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<Tag> obtenerTodas() {
        Query query = em.createQuery("SELECT t FROM Tag t");
        
        return (List<Tag>) query.getResultList();
    }
}
